
#include "common.h"

char* show_hex(uint8_t *data, uint32_t len)
{
    
    size_t lines   = (len + 15) / 16;
    size_t bufsize = len * 3 + lines + 2;
    char *buf      = malloc(bufsize);
    if (!buf) return NULL;
    
    char *p = buf;

    for (uint32_t i = 0; i < len; i++) {
        int w = sprintf(p, "%02x ", data[i]);
        p += w ;
        if ((i + 1) % 16 == 0) {
            printf("\n");
        }
    }
    if (len % 16) {
        *p++ = '\n';
    }
    *p = '\0';  // 终结
    
    // 返回这段字符串
    return buf;
}


void my_print_hex(uint8_t *data, uint32_t len)
{
    char* str = show_hex(data, len);
    printf("%s\n", str);
    free(str);
}
